package com.lc.user.express.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lc.user.express.R;
import com.lc.user.express.app.AppContext;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/3/15.
 */
public class PayKeyboardPopupWindow extends PopupWindow {

    private OnInputFinish onInputFinsh;

    private View mMenuView;

    private LinearLayout ll_container;

    private ArrayList<String> mList = new ArrayList<String>();

    public PayKeyboardPopupWindow(final Activity context) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.layout_popup_window_pay_keyboard, null);
        AppContext.ScaleScreenHelper.loadView((ViewGroup) mMenuView);


        ll_container = (LinearLayout) mMenuView.findViewById(R.id.ll_container);

        this.setContentView(mMenuView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        this.setBackgroundDrawable(dw);
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int heightdown = mMenuView.findViewById(R.id.pop_layout).getBottom();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                    if (y > heightdown) {
                        dismiss();
                    }
                }
                return true;
            }
        });


        ll_container.addView(PayPasswordView.getInstance(context, new PayPasswordView.OnPayListener() {

            @Override
            public void onDelete() {

                // TODO Auto-generated method stub
                if (mList.size() > 0) {
                    ll_container.removeViewAt(mList.size() - 1);
                    mList.remove(mList.get(mList.size() - 1));
                }
                onInputFinsh.onDelete();

            }

            @Override
            public void onInput(String password) {

                // TODO Auto-generated method stub
                onInputFinsh.onInput(password);
//                if (mList.size() <= 0) {
//                    addTextView(context, 1, password);
//                    mList.add(password);
//                } else if (mList.size() == 5) {
//                    addTextView(context, 1, password);
//                    mList.add(password);
//                    onInputFinsh.onfinish(mList);
//                    dismiss();
//                } else if (mList.size() < 6) {
//                    addTextView(context, 0, password);
//                    mList.add(password);
//                }

            }

        }).getView());


    }

    /**
     * @Title: addTextView
     * @Description: TODO
     * @author Administrator
     * @param context
     * @param type
     *            0 两侧子view 1中间子view
     * @param s
     * @date: 2015-10-26 下午3:44:22
     */
    private void addTextView(Activity context, int type, String s) {
        LinearLayout ll = (LinearLayout) View.inflate(context, R.layout.item_key, null);
        if (type == 1) {
            ll.setLayoutParams(new LinearLayout.LayoutParams(AppContext.ScaleScreenHelper.getWidthHeight(116),// 两侧的view稍宽
                    LinearLayout.LayoutParams.WRAP_CONTENT));
        } else {
            ll.setLayoutParams(new LinearLayout.LayoutParams(AppContext.ScaleScreenHelper.getWidthHeight(110),
                    LinearLayout.LayoutParams.WRAP_CONTENT));
        }

        AppContext.ScaleScreenHelper.loadView(ll);

        TextView textView = (TextView) ll.findViewById(R.id.tv_key);
        textView.setText(s);

        ll_container.addView(ll);
    }

    public interface OnInputFinish {
//        void onfinish(List<String> list);
        void onInput(String in);
        void onDelete();
    }


    public void setOnInputFinishListener(OnInputFinish onInputFinsh) {
        this.onInputFinsh = onInputFinsh;
    }


}

